package com.obrempon.credit.market.httptest

import com.obrempon.credit.market.common.CommonActivity
import com.obrempon.credit.market.request.HttpHelper
import com.obrempon.credit.market.request.IHttpCallback
import com.obrempon.module.easyhttp.config.IRequestApi
import com.obrempon.module.easyhttp.listener.OnHttpListener
import org.json.JSONObject


/**
 * ObremponCredit
 * @Author wwdeng
 * @CreateTime: 2025/2/26 11:19
 * @Annotation：
 */
abstract class HttpAct: CommonActivity(), OnHttpListener<Any> {

    /**
     * post 请求
     */
    fun doHttpPostAction(api: IRequestApi,callback: IHttpCallback?){
        HttpHelper.post(api,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                callback?.onRequestSuccess(data)
            }

            override fun onRequestError(error: JSONObject) {
                callback?.onRequestError(error)
                showObremponToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                callback?.onRequestFinish()
            }
        })

//        EasyHttp.post(ApplicationLifecycle.getInstance())
//            .api(api)
//            .request(object : HttpCallbackProxy<String>(this){
//                override fun onHttpSuccess(result: String) {
//                    super.onHttpSuccess(result)
//                    val data = JSONObject(result)
//                    when(data.optInt("code")) {
//                        0 -> {//成功
//                            callback?.onRequestSuccess(data)
//                        }
//                        else -> {
//                            val msg = data.optString("msg")
//                            data.put("errorMsg",msg)
//                            callback?.onRequestError(data)
//                            showObremponToast(msg)
//                        }
//                    }
//                }
//
//                override fun onHttpFail(throwable: Throwable) {
//                    super.onHttpFail(throwable)
//                    val errorObj = JSONObject()
//                    errorObj.put("errorCode",1001)
//                    errorObj.put("errorMsg",throwable.message)
//                    callback?.onRequestError(errorObj)
//                }
//
//                override fun onHttpEnd(api: IRequestApi) {
//                    super.onHttpEnd(api)
//                    callback?.onRequestFinish()
//                }
//            })

        /**
         * 在非Activity或Fragment请求网络
         */
//        EasyHttp.post(ApplicationLifecycle.getInstance())
//            .api(api)
//            .request(object : OnHttpListener<String> {
//                override fun onHttpSuccess(result: String) {
//                    val data = JSONObject(result)
//                    when(data.optInt("code")) {
//                        0 -> {//成功
//                            callback.onRequestSuccess(data)
//                        }
//                        else -> {
//                            callback.onRequestError(data)
//                            val msg = data.optString("msg")
//                            showObremponToast(msg)
//                        }
//                    }
//                }
//                override fun onHttpFail(throwable: Throwable) {
//                    ObremponLogger.e(">>>>>>onHttpFail>>>>>>>>>222>>>>>>>>")
//                }
//            })
    }

    /**
     * 埋点
     */
    fun doEventTrackAction(eventType: String, eventRemark: String? = ""){
//        HttpHelper.setTag(this)
        HttpHelper.addPoint(eventType,eventRemark,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 开始请求
     */
    override fun onHttpStart(api: IRequestApi) {
    }

    /**
     * 请求成功，通常需要单独处理结果。
     */
    override fun onHttpSuccess(result: Any) {
    }

    /**
     * 请求失败
     */
    override fun onHttpFail(throwable: Throwable) {
        showObremponToast(throwable.message)
    }

    /**
     * 请求完成
     */
    override fun onHttpEnd(api: IRequestApi) {
    }
}